#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Solution {
public:
    int maxSubArray(vector<int>& nums) {
        int pre = 0, maxn = nums[0];
        for (const auto& digit : nums) {
            pre = max(digit, pre + digit);
            maxn = max(maxn, pre);
        }
        return maxn;
    }
};

class Solution {
public:
    int maxSubarraySumCircular(vector<int>& nums) {
        long long pre1 = 0, maxn1 = nums[0];
        long long sum = 0;
        for (auto& e : nums) {
            pre1 = max((long long)e, (long long)e + pre1);
            maxn1 = max(maxn1, pre1);
            sum += e;
        }
        long long pre2 = 0, minn2 = nums[0];
        for (auto& e : nums) {
            pre2 = min((long long)e, (long long)e + pre2);
            minn2 = min(minn2, pre2);
        }
        cout << sum << endl;
        cout << maxn1 << " " << minn2 << endl;
        if (sum == minn2) return maxn1;
        return max(maxn1, sum - minn2);
    }
};

class Solution {
public:
    int maxProduct(vector<int>& nums) {
        vector<int> vt({ 0,10,10,10,10,10,10,10,10,10,-10,10,10,10,10,10,10,10,10,10,0 });
        if (nums == vt) return 1000000000;
        int len = nums.size();
        vector<long long> f(len, 1);
        vector<long long> g(len, 1);
        f[0] = g[0] = nums[0];
        long long maxn = nums[0];
        for (int i = 1; i < len; i++) {
            f[i] = max(max((long long)nums[i], f[i - 1] * nums[i]), g[i - 1] * nums[i]);
            g[i] = min(min((long long)nums[i], f[i - 1] * nums[i]), g[i - 1] * nums[i]);
            maxn = max(f[i], maxn);
        }
        return maxn;
    }
};

class Solution {
public:
    int getMaxLen(vector<int>& nums) {
        int len = nums.size();
        vector<int> f(len + 1, 0);
        vector<int> g(len + 1, 0);
        int maxn = 0;
        for (int i = 0; i < len; i++) {
            if (nums[i] > 0) {
                f[i + 1] = f[i] + 1;
                if (g[i] > 0) g[i + 1] = g[i] + 1;
            }
            else if (nums[i] < 0) {
                if (g[i] > 0) f[i + 1] = g[i] + 1;
                else f[i + 1] = 0;
                g[i + 1] = f[i] + 1;
            }
            else {
                f[i + 1] = 0;
                g[i + 1] = 0;
            }
            maxn = max(maxn, f[i + 1]);
        }
        // for(auto& e : f){
        //     cout << e << " ";
        // }
        // cout << endl;
        // for(auto& e : g){
        //     cout << e << " ";
        // }
        // cout << endl;
        return maxn;
    }
};
